package com.szjz.rabbitmq.springboot.topic;

import com.alibaba.fastjson.JSON;
import com.szjz.rabbitmq.springboot.MQConstant;
import com.szjz.rabbitmq.springboot.entity.Order;
import com.szjz.rabbitmq.springboot.entity.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author 史博
 * @desc 动态路由 topic
 */
@Slf4j
@Component
public class TopicConsumer {


    /**
     * 消费者1
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    // 创建临时队列
                    value = @Queue,
                    // 指定换机名称和类型 默认为direct
                    exchange = @Exchange(value = "topicExchange", type = "topic"),
                    // 指定routingKey 接收指定类型的消息
                    key = {"user.*"}
            )
    })
    public void receive1(String msg) {
        Student student = JSON.parseObject(msg, Student.class);
        log.info("==== topic 01 : {}", student);
    }

    /**
     * 消费者2
     */
    @RabbitListener(bindings = {
            @QueueBinding(
                    // 创建临时队列
                    value = @Queue,
                    // 指定换机名称和类型 默认为direct
                    exchange = @Exchange(value = "topicExchange", type = "topic"),
                    // 指定routingKey 接收指定类型的消息
                    key = {"order.#", "order.price"}
            )
    })
    public void receive2(String msg) {
        Order order = JSON.parseObject(msg, Order.class);
        log.info("==== topic 02 : {}", order);
    }
}
